#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

/*================================================
 Program: olsregs.do
 Author:  Avi Ebenstein
 Created: August 2007
 Purpose: OLS regressions
=================================================*/

use ~/pollution/datafiles/dsp_basins.dta, replace;

global controls " share_coal share_firewood share_mining share_tap_water1 yrsed farmer production
share_ferrous share_nonferrous urbcat";
for any manufacturing2 firewood mining paper coal electric: replace share_X=0 if share_X==.;

global controls "yrsed farmer urban lnairpollution i.region i.cla3";

gen lncancer_rate=ln(cancer_o);
gen lndeathrate9=ln(deathrate9);
     
for any 090 091 095: gen lndeathrate_X=ln(deathrate_X);
for any 090 091 095: replace lndeathrate_X=0 if lndeathrate_X==.;

xi: reg lndeathrate9 overall_q [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,replace   se bdec(3) tdec(3);
xi: reg lndeathrate_090 overall_q [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,append   se bdec(3) tdec(3);
xi: reg lndeathrate_091 overall_q [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,append   se bdec(3) tdec(3);
xi: reg lndeathrate_095 overall_q [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,append   se bdec(3) tdec(3);

xi: reg lndeathrate9 overall_q $controls [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,append   se bdec(3) tdec(3);
xi: reg lndeathrate_090 overall_q $controls [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,append   se bdec(3) tdec(3);
xi: reg lndeathrate_091 overall_q $controls [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,append   se bdec(3) tdec(3);
xi: reg lndeathrate_095 overall_q $controls [w=totalpop],cluster(provgb) robust;
outreg2 using ~/pollution/outfiles/olsregs.out,append   se bdec(3) tdec(3);

type ~/pollution/outfiles/olsregs.out;
ex;

use ~/pollution/datafiles/dsp_basins.dta, replace;
global causelist "
maledr_090
maledr_091
maledr_095
femaledr_090
femaledr_091
femaledr_095
";

global mylist "overall_q a_n bod dissolved_ lead mercury oils permanga_n a_n volatile_p";

for any manufacturing2 paper coal electric: replace share_X=0 if share_X==.;

for any 090 091 095: gen lndeathrate_X=ln(deathrate_X);
for any 090 091 095: replace lndeathrate_X=0 if lndeathrate_X==.;

foreach j of global causelist{;

                 gen lndeathrate_`j'=ln(`j');
foreach i of global mylist{;
                           preserve;
                           replace `i'=. if `i'==0;
                           xi: reg lndeathrate_`j' `i' $controls [w=totalpop],cluster(provgb) robust;
                           gen beta=_b[`i'];
                           gen se=_se[`i'];
                           capture drop avg;
                           egen avg=mean(`j');
                           gen t=beta/se;
                           gen str cause="`j'";
                           gen str chemical="`i'";
                           keep cause chemical beta se t avg;
                           keep in 1;
                           save ~/pollution/datafiles/run_`i'_`j', replace;
                           restore;
                         };
               };

clear;
set obs 1;
                 gen a=1;
                 
foreach j of global causelist{;
foreach i of global mylist{;
                           append using ~/pollution/datafiles/run_`i'_`j';
                         };
               };
  
replace chemical="a_a_overall_q" if chemical=="overall_q";
replace cause="a_maledr_090" if cause=="maledr_090";
replace cause="a_maledr_091" if cause=="maledr_091";
replace cause="a_maledr_095" if cause=="maledr_095";
replace cause="b_femaledr_090" if cause=="femaledr_090";
replace cause="b_femaledr_091" if cause=="femaledr_091";
replace cause="b_femaledr_095" if cause=="femaledr_095";

capture log close;
gen dummy=1;
set linesize 250;
log using ~/pollution/logfiles/regs_icd9_3.log, replace;
table chemical cause, c(mean beta mean se) f(%9.3fc);
table dummy cause, c(mean avg) f(%9.3fc);
capture log close;

                 
